import Editor from "../editor";
import { SelectionType } from "../models";
import Operation from "./operation";

export class SetSelectionOperation extends Operation {
  _selection: SelectionType | null;

  constructor(selction: SelectionType | null) {
    super();
    this._selection = selction;
  }

  getSelection(): SelectionType | null{
    return this._selection;
  }

  apply(editor: Editor): void {
    editor.getSelectionModel()?.setSelection(this._selection);
  }

  reverse(): Operation[] {
    return [
      new SetSelectionOperation({ 
        anchor: this._selection?.anchor || 0, 
        focus: this._selection?.focus || 0
      })
    ];
  }
}

export default SetSelectionOperation;